Attribute VB_Name = "modBatch"
'---------------------------------------------------------------------------------------
'Module : modBatch
'Date   : 03/17/2004
'Author : Chris Elwell
'Purpose: Contains the routines required for batch processing.
'         ProcessBatch(ByVal strFile As String) - main routine for processing batch.
'               Uses info on the Batch Input tab to create FXRS transaction.
'
'         WriteBatchReply() - Uses the info on the Batch Output tab in Setup to create
'                             a BatchReply file for the customer.
'         WriteBatchError() - Writes out a file including the error text and the original
'                             data.
'         BatchErrorEdit() - This is called when an item is double clicked in the
'                           Error Transaction (lstErrorTransactions) list box. CIC
'                           can code to map batch fields to screens fields for editing.
'---------------------------------------------------------------------------------------
Option Explicit
Option Compare Text

Public g_strLine As String          'Current line read from batch file
Public g_strTrans As String         'Current batch transaction
'---------------------------------------------------------------------------------------
' Procedure : ProcessBatch(ByVal strFile As String)
' DateTime  : 03/17/2004
' Modified  : 20070327-01 Added ProcessBatch from old v800.2.
' Purpose   : Sets strFile as the input file. Reads a line at a time and assigns the
'             fields in the line to the correct global var. Then calls routines to build
'             and send data. Processing continues when g_blnComplete is set to true in
'             frmShip.g_clsParser_ParseComplete().
' Call from : frmShip.cmdShipAll_Click()
'---------------------------------------------------------------------------------------
'
Public Sub ProcessBatch(ByVal strFile As String, ByVal strType As String)
    Dim strFields() As String
    Dim FSO As New Scripting.FileSystemObject
    Dim fsoFileIn As Scripting.TextStream
    Dim i, j As Integer
    Dim strDelimIn As String
    Dim strQualIn As String
    Dim intUboundInputFlds As Integer
    Dim ret
    Dim strMultiFields() As String
    Dim intCount As Integer
    
    On Error GoTo EH
    
    '20070621-01 Changed BeforeBatch to return a boolean
    If BeforeBatch Then
    
        Set fsoFileIn = FSO.OpenTextFile(strFile, ForReading)
        
        g_lngErrorCount = 0
        g_lngShipCount = 0
        
        Do While Not fsoFileIn.AtEndOfStream
                
            If g_blnAbortBatch Then
                ret = MsgBox("Click OK to resume processing, click Cancel to abort processing.", vbOKCancel, "Batch Processing Paused")
        
                If ret = vbOK Then
                    g_blnAbortBatch = False
                Else
                    Exit Do
                End If
            End If

            'Set the delimiter in the input transaction
            With frmSetup
                'Get total fields configured on Batch Input tab
                For i = 0 To .txtInputFld.UBound
                    If .txtInputFld(i) <> "" Then
                        intUboundInputFlds = i
                    End If
                Next
                Select Case .cboDelimIn
                    Case "Comma"
                        strDelimIn = ","
                    Case "Tab"
                        strDelimIn = vbTab
                    Case "Pipe"
                        strDelimIn = "|"
                    Case "Tilde"
                        strDelimIn = "~"
                End Select
                
                '11/08/05 added qualifier
                Select Case .cboQualIn
                    Case "None"
                        strQualIn = ""
                    Case Chr(34)
                        strQualIn = Chr(34)
                    Case "'"
                        strQualIn = "'"
                End Select
                        
                g_blnComplete = False
                g_strLine = fsoFileIn.ReadLine
                
                'Split the input into an array using the delimiter and qualifier from above
                strFields = Split(g_strLine, strQualIn & strDelimIn & strQualIn)
               
                '11/28/06 Clear out CUST vars
                For i = 0 To 9
                    For j = 0 To 9
                        g_strCUST(i, j) = ""
                    Next
                Next
                
                '20070510-01 Use the smaller of the # of fields in the file or the # configured on Batch Input tab
                intCount = IIf(UBound(strFields) <= intUboundInputFlds, UBound(strFields), intUboundInputFlds)
                
                'Clear transaction string
                g_strTrans = ""
                
                'Deletes
                If UBound(strFields) = 0 Then
                   TransactionBuilder g_strTrans, 0, "023"                        'Transaction type
                   TransactionBuilder g_strTrans, 1, "BatchDelete"                'Transaction ID
                   TransactionBuilder g_strTrans, 29, Trim(strFields(0))          'Trk#
                   TransactionBuilder g_strTrans, 498, Trim(.f498)                'Meter Number
                'Shipping
                Else
                    'Add first fields
                    TransactionBuilder g_strTrans, 0, strType
                    
                    'Added for Call/Express Tags
                    If strType = "044" Then
                        g_strTagType = "CT"
                    ElseIf strType = "046" Then
                        g_strTagType = "ET"
                    Else
                        g_strTagType = ""
                    End If
                    
                    '20070510-01 Changed from intUboundInputFlds to intCount
                    For i = 0 To intCount
                        'Remove the qualifier on first and last fields
                        If i = 0 And strQualIn <> "" Then
                            strFields(i) = Right(strFields(i), Len(strFields(i)) - 1)
                        ElseIf i = UBound(strFields) And strQualIn <> "" Then
                            strFields(i) = Replace(strFields(i), strQualIn, "")
                        End If
                        
                        If Trim(.txtInputFld(i)) <> "N/A" Then
                            'Added 2-D array of 10X10 for CUST fields
                            If Left(.txtInputFld(i), 4) = "CUST" And InStr(.txtInputFld(i), "&") = 0 Then
                                g_strCUST(Val(Mid(.txtInputFld(i), 5, 1)), Val(Right(.txtInputFld(i), 1))) = strFields(i)
                            End If
                            'Remove special characters from ZIP code fields and Sender and Recip phone # fields
                            If .txtInputFld(i) = "17" Or .txtInputFld(i) = "9" Or .txtInputFld(i) = "18" _
                                Or .txtInputFld(i) = "183" Then
                                
                                strFields(i) = RemoveSpecial(strFields(i))
                            
                            End If
                            'Format these fields for 2 implied decimals
                            'Weight, COD Amount, CI Chgs, Dry Ice Wgt
                            If Left(.txtInputFld(i), 4) = "1670" Or .txtInputFld(i) = "53" Or .txtInputFld(i) = "532" _
                                Or .txtInputFld(i) = "533" Or .txtInputFld(i) = "534" Or .txtInputFld(i) = "1684" Then
                                
                                strFields(i) = RemoveSpecial(ImpliedDecimal(strFields(i)))
                            
                            End If
                            'Format Commodity Unit Value for 6 implied decimals, account for multiple occurrence fields
                            If Left(.txtInputFld(i), 4) = "1030" Then
                                strFields(i) = RemoveSpecial(ImpliedDecimal(strFields(i), 6))
                            End If
                            'Format Alcohol Volume for 3 implied decimals
                            If .txtInputFld(i) = "52" Then
                                strFields(i) = RemoveSpecial(ImpliedDecimal(strFields(i), 3))
                            End If
                            'Convert Service Codes if configured in setup
                            If .txtInputFld(i) = "1274" And frmSetup.chkConvertSvcCodes = 1 Then
                                strFields(i) = ConvertServiceType(strFields(i))
                            End If
                            'Convert Country Codes
                            If .txtInputFld(i) = "50" Then
                                strFields(i) = ConvertCountryCode(strFields(i))
                            End If
                            If .txtInputFld(i) = "74" Then
                                strFields(i) = ConvertCountryCode(strFields(i))
                            End If
                            'Added 1365 Dispatch date for CT and ET
                            If (.txtInputFld(i) = "24" Or .txtInputFld(i) = "1365") And IsDate(strFields(i)) Then
                                strFields(i) = Format(strFields(i), "YYYYMMDD")
                            End If
                            'Added 1400 Total Wgt for ET
                            If .txtInputFld(i) = "1400" Then
                                strFields(i) = ImpliedDecimal(strFields(i), 1)
                            End If
                            'Added 1415 Total Dec Val for ET
                            If .txtInputFld(i) = "1415" Then
                                strFields(i) = ImpliedDecimal(strFields(i))
                            End If
                            'Added to allow more than one FXRS field to be populated per position
                            'in the batch file. E.G. One position can populate both transID and references.
                            If InStr(.txtInputFld(i), "&") > 0 Then
                                strMultiFields = Split(.txtInputFld(i), "&")
                                For j = 0 To UBound(strMultiFields)
                                    'Added 2-D array of 10X10 for CUST fields
                                    If Left(strMultiFields(j), 4) = "CUST" Then
                                        g_strCUST(Val(Mid(strMultiFields(j), 5, 1)), Val(Mid(strMultiFields(j), 7, 1))) = strFields(i)
                                    Else
                                        TransactionBuilder g_strTrans, Trim(strMultiFields(j)), strFields(i)
                                    End If
                                    '******************************************************************************
                                    'Added to save batch to local database. Remove when History comes from Sybase
                                    Select Case strMultiFields(j)
                                        Case "1"
                                            g_str1 = strFields(i)
                                        Case "18"
                                            g_str18 = strFields(i)
                                        Case "1670"
                                            g_str1670 = strFields(i)
                                        Case "25"
                                            g_str25 = strFields(i)
                                        Case "3056"
                                            g_str3056 = strFields(i)
                                        Case "3057"
                                            g_str3057 = strFields(i)
                                    End Select
                                    '******************************************************************************
                                Next
                            'Add the field and value to the transaction
                            Else
                                '20070314-01 Changed to Instr so "CUST" fields will not be added to the transaction
                                If InStr(.txtInputFld(i), "CUST") = 0 Then
                                    TransactionBuilder g_strTrans, Trim(.txtInputFld(i)), strFields(i)
                                End If
                            End If
                            '******************************************************************************
                            'Added to save batch to local database. Remove when History comes from Sybase
                            Select Case .txtInputFld(i)
                                Case "1"
                                    g_str1 = strFields(i)
                                Case "18"
                                    g_str18 = strFields(i)
                                Case "1670"
                                    g_str1670 = strFields(i)
                                Case "25"
                                    g_str25 = strFields(i)
                                Case "3056"
                                    g_str3056 = strFields(i)
                                Case "3057"
                                    g_str3057 = strFields(i)
                                Case "11"
                                    g_str11 = strFields(i)
                                Case "12"
                                    g_str12 = strFields(i)
                                Case "13"
                                    g_str13 = strFields(i)
                                Case "15"
                                    g_str15 = strFields(i)
                                Case "16"
                                    g_str16 = strFields(i)
                                Case "17"
                                    g_str17 = strFields(i)
                                Case "50"
                                    g_str50 = strFields(i)
                                Case "10"
                                    g_str10 = strFields(i)
                                Case "1274"
                                    g_str1274 = strFields(i)
                                Case "24"
                                    g_str24 = strFields(i)
                                Case "116"
                                    g_str116 = strFields(i)
                                Case "1123"
                                    g_str1123 = strFields(i)
                                'Added these for Batch Call/Express Tags
                                Case "2210"
                                    g_str2210 = strFields(i)
                                Case "1365"
                                    g_str1365 = strFields(i)
                            End Select
                            '******************************************************************************
                        End If
                    Next
                    
                    'Add fields with default values
                    For i = 0 To .txtFieldNum.UBound
                        'Format weight field for 2 implied decimals
                        If .txtFieldNum(i) = "1670" Then
                            TransactionBuilder g_strTrans, .txtFieldNum(i), RemoveSpecial(ImpliedDecimal(.txtValue(i)))
                        ElseIf .txtFieldNum(i) = "24" Then
                            '20071113-01 Fixed overflow error when date was in YYYYMMDD format
                            If InStr(.txtValue(i), "/") > 0 Then
                                g_str24 = Format(.txtValue(i), "YYYYMMDD")
                            Else
                                g_str24 = .txtValue(i)
                            End If
                            TransactionBuilder g_strTrans, .txtFieldNum(i), g_str24
                        ElseIf .txtFieldNum(i) = "1365" Then
                            g_str1365 = IIf(IsDate(.txtValue(i)), Format(.txtValue(i), "YYYYMMDD"), .txtValue(i))
                            TransactionBuilder g_strTrans, .txtFieldNum(i), g_str1365
                        ElseIf Left(.txtFieldNum(i), 4) = "CUST" And InStr(.txtFieldNum(i), "&") = 0 Then
                            g_strCUST(Val(Mid(.txtFieldNum(i), 5, 1)), Val(Right(.txtFieldNum(i), 1))) = .txtValue(i)
                        ElseIf .txtFieldNum(i) <> "" Then
                            TransactionBuilder g_strTrans, .txtFieldNum(i), .txtValue(i)
                        End If
                        '******************************************************************************
                        'Added to save batch to local database. Remove when History comes from Sybase
                        Select Case .txtFieldNum(i)
                            Case "1"
                                g_str1 = .txtValue(i)
                            Case "18"
                                g_str18 = .txtValue(i)
                            Case "25"
                                g_str25 = .txtValue(i)
                            Case "3056"
                                g_str3056 = .txtValue(i)
                            Case "3057"
                                g_str3057 = .txtValue(i)
                            Case "50"
                                g_str50 = .txtValue(i)
                            Case "10"
                                g_str10 = .txtValue(i)
                            ' 2012-05-25, JFW, constant package count
                            Case "116"
                                g_str116 = .txtValue(i)
                            '20071113-02 Removed date since it is captured above
                            Case "1274"
                                g_str1274 = .txtValue(i)
                            Case "1123"
                                g_str1123 = .txtValue(i)
                            Case "2210"
                                g_str2210 = .txtValue(i)
                        End Select
                        '******************************************************************************
                    Next
                End If
            End With
            
            If g_str24 = "" Then g_str24 = Format(Date, "YYYYMMDD")
            
            'Batch error checking
            If frmShip.chkBatchErrorCheck = 1 Then
                TransactionBuilder g_strTrans, 1234, "3"
                TransactionBuilder g_strTrans, 184, "Y"
            End If
            
            If g_blnUseLabelFldsInBatch Then
                TransactionBuilder g_strTrans, 187, g_str187                          'Label Type
                TransactionBuilder g_strTrans, 537, g_str537                          'Thermal/Laser Label prt UNC/File Path
                If g_str187 <> "DIB" And g_str187 <> "PNG" Then
                    TransactionBuilder g_strTrans, 1282, g_str1282                    'Printer Type Indicator
                End If
                TransactionBuilder g_strTrans, 1660, g_str1660                        'Custom label flag
            End If
                
            'Add 99, ""
            TransactionBuilder g_strTrans, 99, ""
             
            '20070822-02 Modified BeforeBatchShip to return a Boolean.
            If BeforeBatchShip Then
            
                Call SendData(g_strTrans)
            
            End If
            
            '10/28/05 added to control flow if doing batch w/returns
            Do Until g_blnComplete And Not g_blnBatchReturn
                DoEvents
            Loop
        Loop
    
    End If
    
Release:
    Set fsoFileIn = Nothing
    Set FSO = Nothing
    
    Call AfterBatch
    
    Exit Sub
EH:
    MsgBox Err.Description, vbCritical, "modBatch:ProcessBatch"
    Resume Release
    
End Sub
'---------------------------------------------------------------------------------------
' Procedure : WriteBatchReply()
' DateTime  : 2/18/2004
' Purpose   : Use this routine to handle FXRS reply data
' Call from : frmShip.g_clsParser_ParseComplete()
'---------------------------------------------------------------------------------------
'
Public Sub WriteBatchReply()
    Dim strPath As String
    Dim strLineOut As String
    Dim strDelimOut As String
    Dim strQualOut As String
    Dim intUboundOutputFlds As Integer
    Dim i As Integer
    
    On Error GoTo EH
    
    With frmSetup
        'Get the number of output fields
        For i = 0 To .txtOutputFld.UBound
            If .txtOutputFld(i) <> "" Then intUboundOutputFlds = i
        Next
        'Get the output delimiter
        Select Case .cboDelimOut
            Case "Comma"
                strDelimOut = ","
            Case "Tab"
                strDelimOut = vbTab
            Case "Pipe"
                strDelimOut = "|"
            Case "Tilde"
                strDelimOut = "~"
        End Select
        'Get output text qualifier
        strQualOut = IIf(.cboQualOut = "None", "", .cboQualOut)
        
        'Set path to program path or File Path defined is Setup Batch Defaults tab
        strPath = IIf(.txtFilesPath = "", App.Path, .txtFilesPath)
        
        'Add Input Fields to Output file if configured
        If .chkAddInputToOutput = 1 Then strLineOut = g_strLine & strDelimOut
        
        For i = 0 To intUboundOutputFlds
            'Added reference fields since these fields aren't returned in the reply transaction
            If .txtOutputFld(i) = "25" Then
                strLineOut = strLineOut & strQualOut & g_str25 & strQualOut & strDelimOut
            ElseIf .txtOutputFld(i) = "3056" Then
                strLineOut = strLineOut & strQualOut & g_str3056 & strQualOut & strDelimOut
            ElseIf .txtOutputFld(i) = "3057" Then
                strLineOut = strLineOut & strQualOut & g_str3057 & strQualOut & strDelimOut
            '6/01/06 Added to allow getting fields from the input trans that are not returned in the output trans
            ElseIf TransactionParser(g_strSaveReply, .txtOutputFld(i)) = "" Then
                strLineOut = strLineOut & strQualOut & TransactionParser(g_strTrans, .txtOutputFld(i)) & strQualOut & strDelimOut
            Else
                strLineOut = strLineOut & strQualOut & TransactionParser(g_strSaveReply, .txtOutputFld(i)) & strQualOut & strDelimOut
            End If
        Next
    End With
    
    'Remove last delimiter
    strLineOut = Left(strLineOut, Len(strLineOut) - 1)
    
    'Increment shipped counter
    g_lngShipCount = g_lngShipCount + 1
    
    'Update Batch tab with totals
    With frmShip
        .txtNumShipped = Str(g_lngShipCount)
        .txtTotalRecords = Str(g_lngShipCount + g_lngErrorCount)
    End With

    'Write reply fields to output file
    Open strPath & "\BatchReply_" & Format(Date, "YYYYMMDD") & ".txt" For Append As #1
    Print #1, strLineOut
    Close #1
    
    Exit Sub
EH:
    MsgBox Err.Description, vbCritical, "modBatch:WriteBatchReply"
    
End Sub
'---------------------------------------------------------------------------------------
' Procedure : WriteBatchError()
' DateTime  : 2/18/2004
' Purpose   : Use this routine to handle FXRS errors
' Call from : frmShip.g_clsParser_ParseComplete()
'---------------------------------------------------------------------------------------
'
Public Sub WriteBatchError()
    
    Dim strPath As String
    Dim strDelimIn As String
    Dim strQualIn As String
    
    On Error GoTo EH
    
    With frmSetup
        'Set the delimiter in the input transaction
        Select Case .cboDelimIn
            Case "Comma"
                strDelimIn = ","
            Case "Tab"
                strDelimIn = vbTab
            Case "Pipe"
                strDelimIn = "|"
            Case "Tilde"
                strDelimIn = "~"
        End Select
        
        '11/09/05 added qualifier
        Select Case .cboQualIn
            Case "None"
                strQualIn = ""
            Case Chr(34)
                strQualIn = Chr(34)
            Case "'"
                strQualIn = "'"
        End Select
    End With
    'Set path to program path or File Path defined on Setup Batch Defaults tab
    If frmSetup.txtFilesPath = "" Then
        strPath = App.Path
    Else
        strPath = frmSetup.txtFilesPath
    End If
    
    With frmShip
        'Write error message and transaction text to file
        Open strPath & "\BatchErrors_" & Format(Date, "YYYYMMDD") & ".txt" For Append As #1
        'Added code to handle rating errors when performing Batch Error Check
        If g_str3 <> "" Then
           '11/09/05 updated to add qualifier and delimiter from config
            Print #1, strQualIn & g_str3 & strQualIn & strDelimIn & g_strLine
            g_lngErrorCount = g_lngErrorCount + 1
        ElseIf g_str1125 <> "0" And g_str1125 <> "" Then
            If g_str560 <> "" Then
                Print #1, g_str560 & strDelimIn & g_strLine
            Else
                Print #1, GetRateError(g_str1125) & strDelimIn & g_strLine
            End If
            g_lngErrorCount = g_lngErrorCount + 1
        End If
        
        Close #1
        .txtNumErrors = Str(g_lngErrorCount)
        If Not g_blnBatchErrorCheck Then
            '11/09/05 updated to add qualifier and delimiter from config
            .lstErrorTransactions.AddItem strQualIn & g_str3 & strQualIn & strDelimIn & g_strLine
            '6/29/06 only add errors if not Error Checking
            .txtTotalRecords = Str(g_lngShipCount + g_lngErrorCount)
        Else
            '6/29/06 when Error Checking only show ship count.
            .txtTotalRecords = Str(g_lngShipCount)
        End If
    End With
    
    Exit Sub
EH:
     MsgBox Err.Description, vbCritical, "modBatch:WriteBatchError"

End Sub
'---------------------------------------------------------------------------------------
' Procedure : BatchErrorEdit()
' DateTime  : 4/15/2004
' Modified  : 8/9/2004
'           : Added code to apply service type conversion if checked in setup.
' Purpose   : When a line in Error list on the Batch tab is dbl-clicked, this is called
'             and the data is sent to the Ship screen for editing.
' Call from : frmShip.lstErrorTransactions_DblClick()
'---------------------------------------------------------------------------------------
'
'Public Sub BatchErrorEdit(ByVal g_strTrans As String)
'    Dim strFields() As String
'    Dim strMultipleFlds() As String     'Used for positions containing "&"
'    Dim i, j As Integer
'    Dim MyControl As Control
'    Dim strDelimIn As String
'    Dim strQualIn As String
'
'    On Error GoTo EH
'
'    With frmSetup
'        'Set the delimiter in the input transaction
'        Select Case .cboDelimIn
'            Case "Comma"
'                strDelimIn = ","
'            Case "Tab"
'                strDelimIn = vbTab
'            Case "Pipe"
'                strDelimIn = "|"
'            Case "Tilde"
'                strDelimIn = "~"
'        End Select
'
'        '11/08/05 added qualifier
'        Select Case .cboQualIn
'            Case "None"
'                strQualIn = ""
'            Case Chr(34)
'                strQualIn = Chr(34)
'            Case "'"
'                strQualIn = "'"
'        End Select
'
'        '11/09/05 Added qualifier to split
'        strFields = Split(g_strTrans, strQualIn & strDelimIn & strQualIn)
'
'        'If the control name is the same as defined in setup
'        'assign the value in the array to the control
'        For i = 0 To .txtInputFld.UBound
'            '11/09/05 Remove the qualifier on first and last fields
'            If i = 0 And strQualIn <> "" Then
'                strFields(i) = Right(strFields(i), Len(strFields(i)) - 1)
'                '11/09/05 The first field is the error message
'                frmShip.StatusBar1.Panels(3) = strFields(0)
'            ElseIf i = UBound(strFields) And strQualIn <> "" Then
'                strFields(i) = Replace(strFields(i), strQualIn, "")
'            End If
'            For Each MyControl In frmShip.Controls
'                If TypeOf MyControl Is TextBox Or TypeOf MyControl Is ComboBox Then
'                    'Added 7/28 to account for positions with "&"
'                    If InStr(.txtInputFld(i), "&") > 0 Then
'                        strMultipleFlds = Split(.txtInputFld(i), "&")
'                        For j = 0 To UBound(strMultipleFlds)
'                            If MyControl.name = "f" & strMultipleFlds(j) Then
'                                If .cboQualIn = "None" Then
'                                    MyControl = strFields(i + 1)
'                                Else
'                                    MyControl = Replace(strFields(i + 1), .cboQualIn, "")
'                                End If
'                            End If
'                        Next
'                    ElseIf MyControl.name = "f" & .txtInputFld(i) Then
'                        'Added 8/9/04 to apply service type conversions if configured in setup
'                        If MyControl.name = "f1274" And frmSetup.chkConvertSvcCodes = 1 Then
'                            If .cboQualIn = "None" Then
'                                MyControl = ConvertServiceType(strFields(i + 1))
'                            Else
'                                MyControl = ConvertServiceType(Replace(strFields(i + 1), .cboQualIn, ""))
'                            End If
'                        ElseIf .cboQualIn = "None" Then
'                            MyControl = strFields(i + 1)
'                        Else
'                            MyControl = Replace(strFields(i + 1), .cboQualIn, "")
'                        End If
'                    '05/01/06 Added for SP Batch Error Edit
'                    ElseIf MyControl.name = "cbo" & RemoveSpecial(.txtInputFld(i)) Then
'                        frmShip.f1274 = "99"
'                        If .cboQualIn = "None" Then
'                            MyControl = strFields(i + 1)
'                        Else
'                            MyControl = Replace(strFields(i + 1), .cboQualIn, "")
'                        End If
'                    End If
'                End If
'            Next
'        Next
'
'        'If the control name is the same as defined in setup
'        'assign the value in the value field
'        For i = 0 To .txtFieldNum.UBound
'            For Each MyControl In frmShip.Controls
'                If TypeOf MyControl Is TextBox Then
'                    If MyControl.name = "f" & .txtFieldNum(i) Then
'                        MyControl = .txtValue(i)
'                    End If
'                ElseIf TypeOf MyControl Is ComboBox Then
'                    If MyControl.name = "f" & .txtFieldNum(i) And MyControl.Style = 0 Then
'                        MyControl = .txtValue(i)
'                    '05/01/06 Added for SP Batch Error Edit
'                    ElseIf MyControl.name = "cbo" & RemoveSpecial(.txtFieldNum(i)) Then
'                        frmShip.f1274 = "99"
'                        MyControl = .txtValue(i)
'                    End If
'                End If
'            Next
'        Next
'    End With
'
'    Exit Sub
'EH:
'     MsgBox Err.Description, vbCritical, "modBatch:BatchErrorEdit"
'
'End Sub
Public Sub BatchErrorEdit(ByVal g_strTrans As String)
    Dim strFields() As String
    Dim strMultipleFlds() As String     'Used for positions containing "&"
    Dim i, j As Integer
    Dim MyControl As Control
    Dim MyForm As Form
    Dim strDelimIn As String
    Dim strQualIn As String
    
    'On Error GoTo EH
        
    With frmSetup
        'Set the delimiter in the input transaction
        Select Case .cboDelimIn
            Case "Comma"
                strDelimIn = ","
            Case "Tab"
                strDelimIn = vbTab
            Case "Pipe"
                strDelimIn = "|"
            Case "Tilde"
                strDelimIn = "~"
        End Select
        
        '11/08/05 added qualifier
        Select Case .cboQualIn
            Case "None"
                strQualIn = ""
            Case Chr(34)
                strQualIn = Chr(34)
            Case "'"
                strQualIn = "'"
        End Select
        
        '11/09/05 Added qualifier to split
        strFields = Split(g_strTrans, strQualIn & strDelimIn & strQualIn)
            
        'If the control name is the same as defined in setup
        'assign the value in the array to the control
        For i = 0 To .txtInputFld.UBound
            '11/09/05 Remove the qualifier on first and last fields
            If i = 0 And strQualIn <> "" Then
                strFields(i) = Right(strFields(i), Len(strFields(i)) - 1)
                '11/09/05 The first field is the error message
                frmShip.StatusBar1.Panels(3) = strFields(0)
            ElseIf i = UBound(strFields) And strQualIn <> "" Then
                strFields(i) = Replace(strFields(i), strQualIn, "")
            End If
            '20071109-01 Assign CUST fields
            If Left(.txtInputFld(i), 4) = "CUST" And InStr(.txtInputFld(i), "&") = 0 Then
                g_strCUST(Val(Mid(.txtInputFld(i), 5, 1)), Val(Right(.txtInputFld(i), 1))) = strFields(i + 1)
            End If
            '20071109-04 Added to support forms in If statement below
            For Each MyForm In Forms
                If MyForm.name = "frmCOD" Or MyForm.name = "frmHazMat" Or MyForm.name = "frmHAL" Or _
                                MyForm.name = "frmSigReq" Or MyForm.name = "frmAlcohol" Or MyForm.name = "frmShip" Then
                                
                    For Each MyControl In MyForm.Controls
                        If TypeOf MyControl Is TextBox Or TypeOf MyControl Is ComboBox Then
                            'Added 7/28 to account for positions with "&"
                            If InStr(.txtInputFld(i), "&") > 0 Then
                                strMultipleFlds = Split(.txtInputFld(i), "&")
                                For j = 0 To UBound(strMultipleFlds)
                                    If MyControl.name = "f" & strMultipleFlds(j) Then
                                        MyControl = strFields(i + 1)
                                    End If
                                Next
                            ElseIf MyControl.name = "f" & .txtInputFld(i) Then
                                'Added 8/9/04 to apply service type conversions if configured in setup
                                If MyControl.name = "f1274" And frmSetup.chkConvertSvcCodes = 1 Then
                                    MyControl = ConvertServiceType(strFields(i + 1))
                                Else
                                    MyControl = strFields(i + 1)
                                End If
                            '05/01/06 Added for SP Batch Error Edit
                            ElseIf MyControl.name = "cbo" & RemoveSpecial(.txtInputFld(i)) Then
                                frmShip.f1274 = "99"
                                MyControl = strFields(i + 1)
                            End If
                        '20071109-02 Added for Checkboxes
                        ElseIf TypeOf MyControl Is CheckBox Then
                            If MyControl.name = "f" & .txtInputFld(i) Then
                                If strFields(i + 1) = "Y" Then MyControl = 1
                            End If
                        End If
                    Next
                End If
            Next
        Next
        
        'If the control name is the same as defined in setup
        'assign the value in the value field
        For i = 0 To .txtFieldNum.UBound
            '20071109-04 Added to support all forms
            For Each MyForm In Forms
                If MyForm.name = "frmCOD" Or MyForm.name = "frmHazMat" Or MyForm.name = "frmHAL" Or _
                                MyForm.name = "frmSigReq" Or MyForm.name = "frmAlcohol" Or MyForm.name = "frmShip" Then
                                
                    For Each MyControl In MyForm.Controls
                        If TypeOf MyControl Is TextBox Then
                            If MyControl.name = "f" & .txtFieldNum(i) Then
                                MyControl = .txtValue(i)
                            End If
                        ElseIf TypeOf MyControl Is ComboBox Then
                            If MyControl.name = "f" & .txtFieldNum(i) And MyControl.Style = 0 Then
                                MyControl = .txtValue(i)
                            '05/01/06 Added for SP Batch Error Edit
                            ElseIf MyControl.name = "cbo" & RemoveSpecial(.txtFieldNum(i)) Then
                                frmShip.f1274 = "99"
                                MyControl = .txtValue(i)
                            End If
                        '20071109-03 Added for Checkboxes
                        ElseIf TypeOf MyControl Is CheckBox Then
                            If MyControl.name = "f" & .txtFieldNum(i) And .txtValue(i) = "Y" Then
                                MyControl = 1
                            End If
                        End If
                    Next
                End If
            Next
        Next
    End With
    
    Exit Sub
EH:
     MsgBox Err.Description, vbCritical, "modBatch:BatchErrorEdit"

End Sub
'---------------------------------------------------------------------------------------
' Module    : ProcessAddrVerifyBatch
' DateTime  : 9/16/2005
' Author    : Chris Elwell
' Purpose   : Processes Addr Verification Batch shipments
'---------------------------------------------------------------------------------------
'
Public Sub ProcessAddrVerifyBatch(ByVal strFile As String)
    Dim strFields() As String
    Dim FSO As New Scripting.FileSystemObject
    Dim fsoFileIn As Scripting.TextStream
    Dim i As Integer
    Dim j As Integer
    Dim strDelimIn As String
    Dim strQualIn As String
    Dim intUboundInputFlds As Integer
    Dim ret
    Dim strMultiFields() As String
    Dim intCount As Integer
    
    On Error GoTo EH
    
    Set fsoFileIn = FSO.OpenTextFile(strFile, ForReading)
    
    g_lngErrorCount = 0
    g_lngShipCount = 0
    g_lngSoftErrorCount = 0
    
    'CIC defineable routine.
    Call BeforeBatch
    
    Do While Not fsoFileIn.AtEndOfStream
            
        g_blnBatch = True
        
        If g_blnAbortBatch Then
            ret = MsgBox("Click OK to resume processing, click Cancel to abort processing.", vbOKCancel, "Batch Processing Paused")
    
            If ret = vbOK Then
                g_blnAbortBatch = False
            Else
                Exit Do
            End If
        End If
        
        'Set the delimiter in the input transaction
        With frmSetup
            'Get total fields configured on Batch Input tab
            For i = 0 To .txtInputFld.UBound
                If .txtInputFld(i) <> "" Then
                    intUboundInputFlds = i
                End If
            Next
            Select Case .cboDelimIn
                Case "Comma"
                    strDelimIn = ","
                Case "Tab"
                    strDelimIn = vbTab
                Case "Pipe"
                    strDelimIn = "|"
                Case "Tilde"
                    strDelimIn = "~"
            End Select
            
            '11/08/05 added qualifier
            Select Case .cboQualIn
                Case "None"
                    strQualIn = ""
                Case Chr(34)
                    strQualIn = Chr(34)
                Case "'"
                    strQualIn = "'"
            End Select
            
            'Set boolean to false to hold program flow until reply has been received.
            g_blnComplete = False
            
            'Read a line
            g_strLine = fsoFileIn.ReadLine
            
            '11/08/05 Split the input into an array using the delimiter and qualified from above
            strFields = Split(g_strLine, strQualIn & strDelimIn & strQualIn)
                
            '20070510-02 Use the smaller of the # of fields in the file or the # configured on Batch Input tab
            intCount = IIf(UBound(strFields) <= intUboundInputFlds, UBound(strFields), intUboundInputFlds)

            '20070510-02 Changed from intUboundInputFlds to intCount
            For i = 0 To intCount
                '11/08/05 Remove the qualifier on first and last fields
                If i = 0 And strQualIn <> "" Then
                    strFields(i) = Right(strFields(i), Len(strFields(i)) - 1)
                ElseIf i = UBound(strFields) And strQualIn <> "" Then
                    strFields(i) = Replace(strFields(i), strQualIn, "")
                End If
                If Trim(.txtInputFld(i)) <> "N/A" Then
                    strMultiFields = Split(.txtInputFld(i), "&")
                    For j = 0 To UBound(strMultiFields)
                        Select Case UCase(strMultiFields(j))
                            Case "1"
                                If .cboQualIn = "None" Then
                                    g_str1 = strFields(i)
                                Else
                                    g_str1 = Replace(strFields(i), .cboQualIn, "")
                                End If
                            Case "11"
                                If .cboQualIn = "None" Then
                                    g_str11 = strFields(i)
                                Else
                                    g_str11 = Replace(strFields(i), .cboQualIn, "")
                                End If
                            Case "13"
                                If .cboQualIn = "None" Then
                                    g_str13 = strFields(i)
                                Else
                                    g_str13 = Replace(strFields(i), .cboQualIn, "")
                                End If
                            Case "14"
                                If .cboQualIn = "None" Then
                                    g_str14 = strFields(i)
                                Else
                                    g_str14 = Replace(strFields(i), .cboQualIn, "")
                                End If
                            Case "15"
                                If .cboQualIn = "None" Then
                                    g_str15 = strFields(i)
                                Else
                                    g_str15 = Replace(strFields(i), .cboQualIn, "")
                                End If
                            Case "16"
                                If .cboQualIn = "None" Then
                                    g_str16 = strFields(i)
                                Else
                                    g_str16 = Replace(strFields(i), .cboQualIn, "")
                                End If
                            Case "17"
                                If .cboQualIn = "None" Then
                                    g_str17 = strFields(i)
                                Else
                                    g_str17 = Replace(strFields(i), .cboQualIn, "")
                                End If
                        End Select
                    Next
                End If
            Next
                
            '20070822-02 Modified BeforeBatchShip to return a Boolean.
            If BeforeBatchShip Then
            
                Call SendData(BuildAddrVerifyTrans(g_str11, g_str13, g_str14, g_str15, g_str16, g_str17, g_str50))
                Do While Not g_blnComplete
                    DoEvents
                Loop
            
            End If
        
        End With
    Loop
    
    g_blnBatch = False
    
Release:

    Set FSO = Nothing
    Set fsoFileIn = Nothing
    
    'Added 4/6/2005
    Call AfterBatch
    
    Exit Sub
EH:
    MsgBox Err.Description, vbCritical, "modBatch:ProcessAddrVerifyBatch"
    Resume Release
    
End Sub


